﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;
using System.Collections;
using PlanningGIS.Framework;

using DevComponents.DotNetBar;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.SelectQuery
{
    public partial class FrmQryPipePT : Office2007Form
    {
        private List<IFeatureLayer> m_FeatLyrsPipePT;
        private IFeatureLayer m_selectdLayer;
        IApplication m_Application;

        public List<IFeatureLayer> LyrsPipePT
        {
            get { return m_FeatLyrsPipePT; }
            set { m_FeatLyrsPipePT = value; }
        }

        public IApplication Application
        {
            set
            {
                m_Application = value;
            }
            get
            {
                return m_Application;
            }
        }

        public FrmQryPipePT()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string sFldName = "管点编号";
            string sFldValue = cboValue.Text;
            string strWhere = sFldName + " = '" + sFldValue + "'";

            (m_Application as IFramework).MapControl.Map.ClearSelection();
            IFeatureSelection pFeatureSelection =
                CursorHelper.GetFeatureSelection((m_Application as IFramework).MapControl, strWhere, m_selectdLayer.Name);
            if (pFeatureSelection == null)
            {
                MessageBox.Show("未找到指定的管线要素！", "提示");
                return;
            }
            DisplayHelper.ZoomtoSelection((m_Application as IFramework).MapControl, pFeatureSelection);

        }

        private void FrmQryPipePT_Load(object sender, EventArgs e)
        {
            foreach (IFeatureLayer pFeatureLayer in m_FeatLyrsPipePT)
            {
                cboLyrs.Items.Add(pFeatureLayer.Name);
            }
            cboLyrs.SelectedIndex = 0;
        }

        private void cboLyrs_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sLayerName = cboLyrs.Text;
            IMap pMap = (m_Application as IFramework).MapControl.Map;
            m_selectdLayer = MapHelper.GetFirstLayer<IFeatureLayer>(pMap, sLayerName);

            if (m_selectdLayer == null)
            {
                return;
            }

            cboValue.Items.Clear();

            IFeatureClass pFeatCls = m_selectdLayer.FeatureClass;
            IFeatureCursor pFeatCur = pFeatCls.Search(null, false);
            IDataStatistics pDataStatic = new DataStatisticsClass();
            pDataStatic.Field = "管点编号";
            pDataStatic.Cursor = pFeatCur as ICursor;
            IEnumerator pEnumerator = pDataStatic.UniqueValues;
            if (pDataStatic.UniqueValueCount > 2000)
            {
                if (MessageBox.Show("唯一值超过2000个，您确定要全部列出吗？",
                                    "提示", MessageBoxButtons.OKCancel) == DialogResult.No)
                {
                    for (int i = 0; i < 2000; i++)
                    {
                        pEnumerator.MoveNext();
                        cboValue.Items.Add(pEnumerator.Current);
                    }
                    if (cboValue.Items.Count > 0)
                    {
                        cboValue.SelectedIndex = 0;
                    }
                    return;
                }
            }

            while (pEnumerator.MoveNext())
            {
                cboValue.Items.Add(pEnumerator.Current);
            }
            if (cboValue.Items.Count > 0)
            {
                cboValue.SelectedIndex = 0;
            }
        }
    }
}
